﻿using System;
using System.Collections.Generic;

namespace Eflong.Monitor
{
    /// <summary>
    /// 组合报警信息
    /// </summary>
    public class CompositeAlertInfo
    {
        // 矿的ID
        public short CoalMineId = 0;

        // 矿名称
        public string CoalMineName = string.Empty;

        // 组合测点数组
        public List<string> arrIDs = new List<string>( );

        // 组合报警内容
        public string AlarmDescription = string.Empty;

        // 报警开始时刻
        public DateTime tmStart;

        // 报警解除时刻
        public DateTime tmStop;

        // 开始值
        public float beginValue;

        // 结束值
        public float endValue;

        // 风机状态
        public string FJState;

        // 瓦斯状态
        public string WSState;

        // 断电状态  断电\10:19:16\1022
        public string DDState;

        // 馈电状态  无电\10:19:16\1029
        public string KDState;

        // 闭锁状态  闭锁\10:19:16\1029
        public string BSState;

        // 是否馈电异常
        public bool IsFeedAbnormal;

        // 报警日开始时刻
        public DateTime tmDayStart;

        // 报警日解除时刻
        public DateTime tmDayStop;

        // 组合报警原因
        public string Causation;

        // 采用措施
        public string AdoptMeasure;

        //
        public DateTime AdoptMeasureTime;

        // 备注
        public string Note;

        //
        public Int32 AutoId;

        // 巷道断面 m^2
        public float Area;

        // 涌出量的日累计值
        public float sumDayGush;

        // 涌出量的日累计次数

        public float sumDayCount;

        // 涌出量的小时累计值;
        public float sumHourGush;

        // 涌出量的小时累计次数
        public float sumHourCount;

        // 累计一天到了

        public bool IsDayForGush;

        public bool IsDayForGushFirst;

        // 累计开始时刻

        public DateTime tmSumStart;

        // 累计临时时刻
        public DateTime tmSumStop;

        // 累计开始了
        public bool IsStart;

        // 小时平均涌出量
        public float GushHourAvg;

        // 日平均涌出量
        public float GushDayAvg;

        // 最大小时涌出量
        public float GushMax;

        // 最大小时涌出时刻

        public DateTime tmGushMax;

        // 最小小时涌出量
        public float GushMin;

        // 最小小时涌出时刻

        public DateTime tmGushMin;

        // 绝对涌出量

        // 风量 m^3/min
        public float Q;

        // 相对涌出量

        public void Init( )
        {
            IsStart = true;
            IsDayForGushFirst = true;
            Init_Day( );
            Init_Houre( );
        }

        public void Init_Houre( )
        {
            tmSumStart = DateTime.Now;
            tmSumStop = DateTime.Now;
            sumHourGush = 0;
            sumHourCount = 0;
        }

        public void Init_Day( )
        {
            IsDayForGush = false;
            sumDayGush = 0;
            sumDayCount = 0;
            tmDayStart = DateTime.Now;
            tmDayStop = DateTime.Now;
            tmGushMax = DateTime.Now;
            tmGushMin = DateTime.Now;
            GushMax = -10000.0F;
            GushMin = 10000.0F;
        }

        /// <summary>
        /// 积分小时的瓦斯绝对涌出量
        /// </summary>
        /// <param name="fCh4_Value"></param>
        /// <param name="q"></param>
        public void SumHourGush( float fCh4_Value , float q )
        {
            // 积分的单位是 m^3/min
            // 计算的假设条件是,通风良好
            // 如果风机停风了,还有风速吗?
            // 计算时差,单位分钟
            double time = ( double )( DateTime.Now.Subtract( tmSumStop ).TotalMinutes );
            double time_sum = ( double )( DateTime.Now.Subtract( tmSumStart ).TotalMinutes );

            // 积分: 浓度/100 * 风速*60(m/min)*断面(m^2)    m^3/min
            sumHourGush += Convert.ToSingle( time * fCh4_Value / 100.0 * q );
            tmSumStop = DateTime.Now;

            // 计算平均涌出量

            // 60 秒以上,才计算平均值

            if ( time_sum >= 1 )
            {
                GushHourAvg = ( float )Convert.ToInt32( sumHourGush * 100 / time_sum ) / 100;
            }
            else
            {
                GushHourAvg = 0;
            }

            // 再进行日涌出统计
            // SumDayGush();
        }

        /// <summary>
        /// 计算日涌出量
        /// </summary>
        public void SumDayGush( )
        {
            if ( tmSumStop.Hour != tmSumStart.Hour || tmSumStop.Subtract( tmSumStart ).TotalHours >= 1.0 )
            {
                // 超过一个小时了
                sumDayGush += GushHourAvg;
                sumDayCount++;
                GushDayAvg = sumDayGush / sumDayCount;

                if ( GushHourAvg >= GushMax )
                {
                    GushMax = GushHourAvg;
                    tmGushMax = tmSumStart;
                }
                if ( GushHourAvg < GushMin )
                {
                    GushMin = GushHourAvg;
                    tmGushMin = tmSumStart;
                }

                Init_Houre( );
            }
            if ( DateTime.Now.Hour == 8 && IsDayForGushFirst )
            {
                // 每天早上 8 点 10 秒 时设置统计生成标志

                IsDayForGushFirst = false;
                IsDayForGush = true;
                tmDayStop = DateTime.Now;
            }
            else if ( DateTime.Now.Hour != 8 && !IsDayForGushFirst )
            {
                // 每天早上 8 点 10 秒 时设置统计生成标志

                IsDayForGush = false;
                IsDayForGushFirst = true;
            }
        }
    }
}